﻿using System.Collections.Generic;
using CkSoftware.GroupMe.Sdk.Core;
using CkSoftware.GroupMe.Sdk.Core.Dao;
using CkSoftware.GroupMe.Sdk.Core.Exceptions;
using CkSoftware.GroupMe.Sdk.Groups.Commands;
using Newtonsoft.Json;

namespace CkSoftware.GroupMe.Sdk.Groups.Dao
{
	/// <summary>
	/// The parameters for the <see cref="GroupAddMemberCommand" />.
	/// </summary>
	public class GroupAddMemberParameters : IWebApiCommandParameters
	{
		private List<AddMemberData> _members;

		/// <summary>
		/// Instantiate a new <see cref="GroupAddMemberParameters" />.
		/// </summary>
		public GroupAddMemberParameters()
		{
			_members = new List<AddMemberData>();
		}

		/// <summary>
		/// The list of members to be added to the group.
		/// </summary>
		[JsonProperty(PropertyName = "members")]
		public IEnumerable<AddMemberData> Members
		{
			get { return _members; }
			set { _members = new List<AddMemberData>(value); }
		}

		/// <summary>
		/// Adds a new member data to the list of members.
		/// </summary>
		/// <param name="memberData">The <see cref="AddMemberData"/> object. IsValid will be executed before adding.</param>
		/// <exception cref="GroupMeException">Occurs if the memberData is not valid.</exception>
		/// <returns>The data of the added member.</returns>
		public AddMemberData AddMember(AddMemberData memberData)
		{
			if (!memberData.IsValid())
			{
				throw new GroupMeException(new MetaData
				{
					Code = 500,
					Errors =
						new[]
						{
							"The given member data is not valid for an AddMember command. Please review that the member contains at least a nickname and one of userId, phoneNumber or email."
						}
				});
			}

			_members.Add(memberData);

			return memberData;
		}
	}
}